<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	<link rel="stylesheet" href="{{__PUBLIC__}}/Bootstrap/Css/bootstrap.min.css" />
	<link rel="stylesheet" href="{{__PUBLIC__}}/Admin/Css/common.css" />
	<script type="text/javascript" src='{{__PUBLIC__}}/jquery-1.7.2.min.js'></script>
	<script type="text/javascript">
		$(function(){
			$('select[name=category_cid]').change(function(){
				//获得被选中的tid
				var tid=$(':selected',this).attr('tid');
				if(tid == 0) return;
				$.ajax({
					type:"post",
					url:'{{U('ajaxGetAttr')}}',
					data:{tid:tid},
					dataType:'json',
					success:function(phpData){
						
						//属性
						var attr = '';
						//规格
						var spec = '';
						var attr1='';
						$.each(phpData,function(k, v) {
							//如果是属性时
							if(v.tatype==0){
								attr += '<tr class="info"><td align="right">'+v.taname+'</td><td>';
								attr += '<select name="attr['+v.taid+']" id=""><option value="">请选择</option>';
									$.each(v.tvalue,function(kk, vv) {
										attr += '<option value="'+vv+'">'+vv+'</option>';
									});
								attr += '</select></td></tr>'; 
							}else{//否则就是规格
								
								spec += '<tr class="info"><td align="right">'+v.taname+'</td><td>';
								spec += '<select name="spec['+v.taid+'][value][]" id=""><option value="">请选择</option>';
									$.each(v.tvalue,function(kk, vv) {
										spec += '<option value="'+vv+'">'+vv+'</option>';
									});
								spec += '</select></td>';
								spec += '<td>附加价格：<input type="text" name="spec['+v.taid+'][price][]" value="0" /></td>'
								spec += '<td><span class="add-spec btn btn-success"><i class="icon-plus icon-white"></i>添加规格</span></td>';
								spec += '</tr>'; 
							}
							attr1 +='<input type="hidden" name="type_tid" value="'+v.type_tid+'"/>';
						});
						$('#attr').html(attr);
						$('#t1').html(attr1);
						$('#spec').html(spec);
					}
				});
			});
			//点击增加规格
			$('.add-spec').live('click',function(){
				//找到父级的tr
				var tr = $(this).parents('tr');
				//克隆它
				var trObj = tr.clone();
				$('.add-spec',trObj).removeClass('btn-success add-spec').addClass('btn-danger del-spec').html('<i class="icon-minus icon-white"></i>删除规格');
				//在它的下一行插入
				tr.after(trObj);

			})
			//删除规格
			$('.del-spec').live('click',function(){
				$(this).parents('tr').remove();
			});
			$('.close_img1').click(function(){
					//1.关闭图片，x也消失
					$('.img_div1').remove();
					//2.显示上传表单
					$('#d1').show();
			});
			$('.close_img').click(function(){
					//1.关闭图片，x也消失
					$('.img_div').remove();
					//2.显示上传表单
					$('#d2').show();
			});
		});
	</script>
	</head>
	<body>
	<div class="container-fluid">
	<div class="row-fluid">
	<div class="span12">

	<form action="" method='post' enctype="multipart/form-data">
		<fieldset>
			<legend>添加商品</legend>
			<table class='table table-bordered table-hover'>
				<thead>
					<tr>
						<th colspan="2" class="btn btn-primary">基本信息</th>
					</tr>
				</thead>
				<tbody>
					<tr class="info">
						<td>所属分类</td>
						<td>
						
							<select name="category_cid">
								<option value="0">-请选择-</option>
								{{foreach from="$cate" value="$v"}}
									<option value="{{$v['cid']}}" tid="{{$v['type_tid']}}" {{if value="$v['cid'] eq $oldData['category_cid']"}}selected{{endif}}>{{$v['_name']}}</option>
								{{endforeach}}
							</select>
						</td>
					</tr>
					<tr class="info">
						<td>所属品牌</td>
						<td>
							<select name="brand_bid">
								<option value="0">-请选择-</option>
								{{foreach from="$brand" value="$v"}}
									<option value="{{$v['bid']}}" {{if value="$v['bid'] eq $oldData['brand_bid']"}}selected{{endif}}>{{$v['bname']}}</option>
								{{endforeach}}
								
							</select>
						</td>
					</tr>
					<tr class="info">
						<td>商品名称</td>
						<td>
							<input type="text" name='gname' value="{{$oldData['gname']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>货号</td>
						<td>
							<input type="text" name='artno' value="{{$oldData['artno']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>单位</td>
						<td>
							<input type="text" name='unit' value='件' value="{{$oldData['unit']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>市价场</td>
						<td>
							<input type="text" name='price' value="{{$oldData['price']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>商城价</td>
						<td>
							<input type="text" name='sale' value="{{$oldData['sale']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>货存量</td>
						<td>
							<input type="text" name='stocks' value="{{$oldData['stocks']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>点击次数</td>
						<td>
							<input type="text" name='ctr' value="{{$oldData['ctr']}}"/>
						</td>
					</tr>
				</tbody>
			</table>

			<p class="btn btn-primary">商品属性</p>
			<table class="table table-bordered table-hover" id='attr' class="info">
				{{foreach from="$goodsatt" value="$v"}}
				{{if value="$v['tatype'] eq 0"}}
				<tr class="info">
					<td align="right">{{$v['taname']}}</td><td>
						<select name="attr[{{$v['taid']}}]" id="">
						<option value="">请选择</option>
						{{foreach from="$v['tvalue']" value="$vv"}}
						<option value="{{$vv}}" {{if value="$vv eq $v['gavalue']"}}selected{{endif}}>{{$vv}}</option>
						{{endforeach}}
						</select>
					</td>
				</tr> 
				{{endif}}
				{{endforeach}}
			</table>
			<p class="btn btn-primary">商品规格</p>
			<table class="table table-bordered table-hover" id='spec'>
				{{foreach from="$goodsatt" value="$v"}}
				{{if value="$v['tatype'] eq 1"}}
				<tr class="info">
					<td align="right">{{$v['taname']}}</td><td>
						<select name="spec[{{$v['taid']}}][value][]" id="">
						<option value="">请选择</option>
						{{foreach from="$v['tvalue']" value="$vv"}}
						<option value="{{$vv}}" {{if value="$vv eq $v['gavalue']"}}selected{{endif}}>{{$vv}}</option>
						{{endforeach}}
						</select>
					</td>
					<td>附加价格：<input type="text" name="spec[{{$v['taid']}}][price][]" value="{{$v['gprice']}}" /></td>
					<td><span class="add-spec btn btn-success"><i class="icon-plus icon-white"></i>添加规格</span></td>
				</tr> 
				{{endif}}
				{{endforeach}}
			</table>

			<table class='table table-bordered'>
				<tr>
					<th colspan="3" class="btn btn-primary">列表图</th>
				</tr>
				<tr class="info">
					<td>上传图片</td>
					<td id="in0">
						{{if value="$oldData['tabular']"}}
							<div class="img_div1">
								<img src="{{__ROOT__}}/{{$oldData['tabular']}}"/>
								<a href="javascript:;" class="close_img1">X</a>
								<input type="hidden" name="tabular" value="{{$oldData['tabular']}}"/>
							</div>
						{{else}}
							<input type="file" name='tabular' id='pic'/>
						{{endif}}
						<div lab="uploadFile" id="d1" style="display: none;">
						  <input id="file" type="file" multiple="true">
						    <span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
						    <script type="text/javascript" src='{{__PUBLIC__}}/uploadify/jquery.uploadify.min.js'></script>
						    <script type="text/javascript">
						        $(function() {
						            $('#file').uploadify({
						                'formData'     : {//POST数据
						                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
						                },
						                'fileTypeDesc' : '上传文件',//上传描述
						                'fileTypeExts' : '*.jpg;*.png',
						                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
						                'uploader' : '{{U('upload')}}',
						                'buttonText':'选择文件',
						                'fileSizeLimit' : '2000KB',
						                'uploadLimit' : 10,//上传文件数
						                'width':65,
						                'height':25,
						                'successTimeout':10,//等待服务器响应时间
						                'removeTimeout' : 0.2,//成功显示时间
						                'onUploadSuccess' : function(file, data, response) {
											//转为json
						                    data=$.parseJSON(data);
						                    //获得图片地址
						                    var imageUrl = data.url;
						                    var li="<li path='"+data.path+"' url='"+data.url+"'><img src='"+imageUrl+"'/><input type='hidden' name='tabular' value='"+data.path+"'/></li>";
						                    $("#uploadList ul").prepend(li);
						                }
						            });
						        });
						    </script>
						    <div id="uploadList">
						        <ul>
						
						        </ul>
						    </div>
						</div>
					</td>
					<td id='pic-list'></td>
				</tr>
			</table>

			<table class='table table-bordered'>
				<tr>
					<th colspan="3" class="btn btn-primary">商品图册</th>
				</tr>
				<tr class="info">
					<td>上传图片</td>
					<td id="in1">
						{{if value="$oldData['photo']"}}
						{{foreach from="$photo" value="$v"}}
							<div class="img_div">
								<img src="{{__ROOT__}}/{{$v}}"/>
								<a href="javascript:;" class="close_img">X</a>
								<input type="hidden" name="photo[]" value="{{$v}}"/>
							</div>
						{{endforeach}}
						{{else}}
							<input type="file" name='photo' id='photo' />
						{{endif}}	
					<div lab="uploadFile" id="d2" style="display: none;">
						    <input id="file1" type="file" multiple="true">
						    <span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
						     <script type="text/javascript">
						        $(function() {
						            $('#file1').uploadify({
						                'formData'     : {//POST数据
						                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
						                },
						                'fileTypeDesc' : '上传文件',//上传描述
						                'fileTypeExts' : '*.jpg;*.png;*.gif',
						                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
						                'uploader' : '{{U('upload')}}',
						                'buttonText':'选择文件',
						                'fileSizeLimit' : '2000KB',
						                'uploadLimit' : 10,//上传文件数
						                'width':65,
						                'height':25,
						                'successTimeout':10,//等待服务器响应时间
						                'removeTimeout' : 0.2,//成功显示时间
						                'onUploadSuccess' : function(file, data, response) {
											//转为json
						                    data=$.parseJSON(data);
						                    //获得图片地址
						                    var imageUrl = data.url;
						                    var li="<li path='"+data.path+"' url='"+data.url+"'><img src='"+imageUrl+"'/><input type='hidden' name='photo[]' value='"+data.path+"'/></li>";
						                    $("#uploadList1 ul").prepend(li);
						                }
						            });
						        });
						    </script>
						    <div id="uploadList1">
						        <ul>
						
						        </ul>
						    </div>
						</div>	
					</td>
					<td id='photo-list'></td>
				</tr>
			</table>

			<table class='table'>
				<tr class="next_show">
					<th class="btn btn-primary">商品详细</th>
				</tr>
				<tr class="info">
					<td>
						<textarea name="intro" id="intro">{{$oldData['intro']}}</textarea>
					</td>
				</tr>
			</table>

			<table class='table'>
				<tr class="next_show">
					<th class="btn btn-primary">售后服务</th>
				</tr>
				<tr class="info">
					<td>
						<textarea name="service" id="service">{{$oldData['service']}}</textarea>
					</td>
				</tr>
			</table>
			<p id="t1">
				<input type="hidden" name="type_tid" value="{{$oldData['type_tid']}}"/>
			</p>
			<input type="submit" value="确认编辑" class="btn btn-primary btn-block btn-large" />
		</fieldset>
	</form>

	</div>
	</div>
	</div>
</body>
</html>
